// pages/mode/mode.js
// import Dialog from 'path/to/@vant/weapp/dist/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tableList: [],//总数据
    result: [],//已选择的答案
    num: 0, //记录当前索引
    time: 2 * 60 * 1000,
    timer: 30,//设置时间 （单位分钟）
    textTime: '',//时间转换的字符串
    bfb: 0,//百分比时间
    show: false,//控制弹出框
    shows: false,//控制弹出框
    numtotal: 0,//记录分数
    remaining:0,//记录用时
  },
  //统计得分后 点击确定
  getUserInfo(event) {
    console.log(event.detail);
  },
  //得分后 取消按钮
  onClose() {
    this.setData({
      shows: false
    });
  },
  //提交
  submit() {
    // this.timers('a')
    // this.data.numtotal = 0
    let tableList = this.data.tableList;
    let numtotal = this.data.numtotal;
    let score = 100/this.data.tableList.length;
    tableList.forEach((item) => {
      if (item.answer) {
        if (item.answer == item.answers) {
          // console.log("Y")
          return numtotal += score
        }
      }
    })
    this.setData({
      numtotal: numtotal,
      shows: true,
    })
  },
  //点击序列号跳转
  serial(event) {
    // console.log(event.target.dataset.serial)
    this.setData({
      num: event.target.dataset.serial,
      // show: false,
      result: []
    })
    if(this.data.tableList[this.data.num].answer){
      console.log(1)
      this.setData({
        result: JSON.parse(this.data.tableList[this.data.num].answer)
      })
      // this.data.result = JSON.parse(this.data.tableList[this.data.num].answer)
    }
  },
  //显示
  showPopup() {
    this.setData({
      show: true
    });
  },
  //隐藏
  onClose() {
    this.setData({
      show: false
    });
  },
  //计算时间
  timers(val) {
    console.log(val)
    let oldTime = this.data.timer * 60
    let newtimers = oldTime;
    let newtimer = setInterval(() => {
      let hours = Math.floor(newtimers / 3600);
      let min = Math.floor(newtimers / 60);
      let s = newtimers % 60;
      if(this.data.shows){
        clearInterval(newtimer)
        
        this.setData({
          remaining:this.fill0(Math.floor((oldTime-newtimers)/60))+":"+this.fill0((oldTime-newtimers) % 60)
        })
      }else{
        if (newtimers > 0) {
          newtimers--;
        } else {
          clearInterval(newtimer)
          this.submit()
        }
      }

      this.setData({
        timer: newtimers,
        textTime: this.fill0(hours) + ":" + this.fill0(min) + ":" + this.fill0(s),
        bfb: Math.floor(newtimers / oldTime * 100)
      })
      // console.log(hours + ":" + min + ":" + s)
    }, 1000)
  },
  // 补零
  fill0(val) {
    return val < 10 ? '0' + val : val
  },
  //复选框
  onChange(event) {
    // console.log(event.detail)
    let num = this.data.num;
    this.setData({
      result: event.detail,
    });
    // console.log(this.data.result.sort())
    this.setData({
      ['tableList[' + num + '].answer']: JSON.stringify(this.data.result.sort())
    });
    // console.log(this.data.tableList[num].answer)

  },
  //下一题
  nextt() {
    if (this.data.num < this.data.tableList.length - 1) {
      // console.log(this.data.result)
      this.setData({
        result: [],
        num: this.data.num += 1,
      })
      if (this.data.tableList[this.data.num].answer) {
        this.setData({
          result: JSON.parse(this.data.tableList[this.data.num].answer)
        })
      }
    }
    console.log(this.data.tableList)
  },
  //上一题
  restore() {
    let num = this.data.num;
    if (num > 0) {
      this.setData({
        num: num -= 1,
      })
      if (this.data.tableList[num].answer) {
        this.setData({
          result: JSON.parse(this.data.tableList[num].answer)
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  //拉数据
  onLoad: function (options) {
    this.timers()
    wx.request({
      url: 'http://localhost:3001/questions/randList',
      data: {
        pageSize:this.data.pageSize
      },
      header: {},
      success: (res) => {
        let resData = res.data.data;
        resData.forEach(item => {
          item.options = JSON.parse(item.options)
          // item.answer = []
          // console.log(item)
        })
        this.setData({
          tableList: resData
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})